<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('物料领用录入')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet">
</head>
<style>
    input {
        border: 0px;
        outline: none;
    }

</style>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <form class="form-horizontal" method="post" id="materialLead">
        <div class="row">
            <div class="form-group">
                <div class="col-sm-8" style="margin-left: 16px">
                    <input class="btn btn-primary" type="button" onclick="save()" value="保存">
                    <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取消</button>
                </div>
            </div>
            <!-- 显示 隐藏测试 -->
            <ul id="myTab" class="nav nav-tabs">
                <li class="active">
                    <a href="#basicinformation" linkTab="basicinformation" isActive="true" data-toggle="tab"> 基础信息</a>
                </li>
            </ul>
            <!-- 基本信息 -->
            <!--startprint-->
            <div class="table-responsive">
                <table class="table table-bordered text-nowrap" id="material_table">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>物料号</th>
                        <th>物料描述</th>
                        <th>工厂</th>
                        <th>可用数量</th>
                        <th>领用数量</th>
                        <th>领用部门</th>
                        <th>预计归还时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="tbMaterial">
                    <tr th:each="materialLead,itemStat:${itemList}" th:class="${itemStat.odd}?'odd':'even'">
                        <td width='30px' th:text="${itemStat.index+1}"></td>
                        <td style="display:none"><input id="materialLeadId" name="materialLeadId" type="hidden"
                                                        th:value="${materialLead.materialLeadId}"/></td>
                        <td width='30px'><input id="materialCode" name="materialCode"
                                                th:value="${materialLead.materialCode}" readonly="readonly"/></td>
                        <td width='30px'><input id="materialDesc" name="materialDesc"
                                                th:value="${materialLead.materialDesc}" readonly="readonly"/></td>
                        <td width='30px'><input id="werks" name="werks" th:value="${materialLead.werks}"
                                                readonly="readonly"/></td>
                        <td width='30px'><input id="usecont" name="usecont" th:value="${materialLead.usecont}"
                                                readonly="readonly"/></td>
                        <td width='30px'><input class="leadnum" name="leadnum" type="number" /></td>
                        <td width='200px'><select style="width: 100%" class="form-control" id="leadDept" name="leadDept"
                                                  sysDict="LYBM"
                                                  emptyFirst="false"></select></td>
                        <td width='30px'><input id="returntime" name="returntime"/></td>
                        <td width='30px'>
                            <button class="btn btn-danger" type="button">删除</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!--endprint-->
            </div>
        </div>
    </form>
</div>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>
<script type="text/javascript">
    // 初始化时间插件
    laydate.render({elem: '#returntime'});
    //初始化所有字典数据
    initDictDataSel();

    //保存
    function save() {
        layer.load(1,{shade: [0.3, '#393D49']});
        var data = [];
        var trs = $("#tbMaterial").find("tr");
        //定义一个角标数量
        var intNum=$("#tbMaterial").find("tr").length;
        var filename="";
            //定义成功标记
        var flg=true;
        trs.each(function (index, tr) {
            var itemEntity = {};
            var its = $(tr).find("input, select");
            its.each(function (idx, it) {//循环遍历，把item的内容塞进一个item对象，然后加到数组里
                itemEntity[$(it).attr("name")] = $(it).val();
                if($(it).attr("name")=="leadnum"){
                    if($(it).val()==""){
                       Dialog.errorMsg("领用数量不可以为空!");
                        flg=false;
                    }
                }
            });
            data.push(itemEntity);
        });
        if(!flg){
            return;
        }
      $.ajax({
            type: "get",
            url: ctxPath+"/materialFactory/save",
            data: {"datalist": JSON.stringify(data)},
            dataType: "json",
            success: function (res) {
                if(res!=null&&res!=''){
                    parent.deleteNum(intNum);
                    parent.refresh();
                    Dialog.closeFrame(window.name);
                    layer.closeAll();
                }else{
                    Dialog.errorMsg("导出失败!");
                }

            }
        });

    }

    /**
     * @Description: 控制安全库存数量和库存数量
     */
    $(".leadnum").change(function () {
        var usecont=$(this).parent().parent().find("#usecont").val();
        var leadnum=$(this).val();
        if(leadnum==0){
            Dialog.errorMsg("领用数量不可为0!")
        }
        if(parseInt(leadnum)>parseInt(usecont)){
            Dialog.errorMsg("领用数量不可大于可用库存!");
            $(this).val("");
        }
    });
    /**
     * @Description: 删除领料单
     */
    $(".btn-danger").click(function () {
        var pkid=$(this).parent().parent().find("#materialLeadId").val();
        $(this).parent().parent().remove();
        Dialog.confirm('确定删除？', function (index) {
            Page.ajaxPOST(ctxPath + "/materialLead/delateMaterial", {pkid: pkid}, function () {
                Dialog.msg("删除成功!", function () {
                    parent.deleteNum(1);
                    parent.refresh();
                });
            });
        }, {"loadingMsg" : "正在往服务器提交数据……"});
    });
    function doPrint() {
        bdhtml = window.document.body.innerHTML;
        sprnstr = "<!--startprint-->"; //开始打印标识字符串有17个字符
        eprnstr = "<!--endprint-->"; //结束打印标识字符串
        prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17); //从开始打印标识之后的内容
        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr)); //截取开始标识和结束标识之间的内容
        window.document.body.innerHTML = prnhtml; //把需要打印的指定内容赋给body.innerHTML
        window.print(); //调用浏览器的打印功能打印指定区域
        Dialog.closeFrame(window.name);
    }
</script>
</body>
</html>
